//
//  PywSelectCouponCell.m
//  PywSdk
//
//  Created by liangyidong on 16/3/5.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywSelectCouponCell.h"
#import "PywMasonry.h"
#import "UIImage+Pyw.h"
#import "PywCoupon.h"
#define MoneyColor(select) (select)?UIColorFromRGB(0xF6BA3E):UIColorFromRGB(0xD6D6D6)
#define TopViewColor(select) (select)?[UIColor orangeColor]:UIColorFromRGB(0xCCCCCC)
@interface PywSelectCouponCell()
@property (nonatomic,strong) UIView *topView;
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *moneyLabel;
@property (nonatomic,strong) UIView *lineView;
@property (nonatomic,strong) UILabel *descriptionLabel;
@property (nonatomic,strong) UILabel *dateLabel;
@property (nonatomic,strong) UIButton *selectButton;


@end

@implementation PywSelectCouponCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark - setUp

- (void)setUp
{
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.bottomView];
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.moneyLabel];
    [self.backView addSubview:self.lineView];
    [self.backView addSubview:self.descriptionLabel];
    [self.backView addSubview:self.dateLabel];
    [self.backView addSubview:self.selectButton];
    
    [self addconstrants];
}

- (void)addconstrants
{
    WEAK_SELF;
    [self.topView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.contentView.mas_top);
        make.height.mas_equalTo(30);
        make.left.equalTo(strongSelf.contentView.mas_left).offset(10);
        make.right.equalTo(strongSelf.contentView.mas_right).offset(-10);
    }];
    
    [self.bottomView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(_topView.mas_bottom);
        make.bottom.equalTo(strongSelf.mas_bottom);
        make.left.equalTo(strongSelf.mas_left).offset(10);
        make.right.equalTo(strongSelf.mas_right).offset(-10);
    }];
    
    [self.backView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.contentView.mas_top).offset(5);
        make.bottom.equalTo(strongSelf.contentView.mas_bottom).offset(-5);
        make.left.equalTo(strongSelf.contentView.mas_left).offset(10);
        make.right.equalTo(strongSelf.contentView.mas_right).offset(-10);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.backView.mas_top);
        make.bottom.equalTo(strongSelf.backView.mas_bottom);
        make.width.mas_equalTo(90);
        make.left.equalTo(strongSelf.backView.mas_left).offset(5);
    }];
    
    [self.lineView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.backView.mas_top).offset(10);
        make.bottom.equalTo(strongSelf.backView.mas_bottom).offset(-5);
        make.left.equalTo(strongSelf.moneyLabel.mas_right);
        make.width.mas_equalTo(1);
    }];
    
    [self.descriptionLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.backView.mas_top).offset(5);
        make.height.mas_equalTo(20);
        make.left.equalTo(strongSelf.moneyLabel.mas_right).offset(10);
        make.right.equalTo(strongSelf.backView.mas_right).offset(-50);
    }];
    
    [self.dateLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.top.equalTo(strongSelf.descriptionLabel.mas_bottom);
        make.height.mas_equalTo(20);
        make.left.equalTo(strongSelf.descriptionLabel.mas_left);
        make.right.equalTo(strongSelf.descriptionLabel.mas_right);
    }];
    
    [self.selectButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.centerY.equalTo(strongSelf.backView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.right.equalTo(strongSelf.backView.mas_right).offset(-10);
    }];
}


#pragma mark - getter

- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = TopViewColor(YES);
        _topView.layer.cornerRadius = 4.f;
        _topView.layer.masksToBounds = YES;
    }
    return _topView;
}

- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.layer.cornerRadius = 4.f;
        _bottomView.layer.masksToBounds = YES;
    }
    return _bottomView;
}

- (UIView *)backView
{
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UILabel *)moneyLabel
{
    if (!_moneyLabel) {
        _moneyLabel = [UILabel new];
        _moneyLabel.font = [UIFont boldSystemFontOfSize:20];
        _moneyLabel.textColor = MoneyColor(NO);
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _moneyLabel;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = UIColorFromRGB(0xF4F4F4);
    }
    return _lineView;
}

- (UILabel *)descriptionLabel
{
    if (!_descriptionLabel) {
        _descriptionLabel = [UILabel new];
        _descriptionLabel.textColor = UIColorFromRGB(0xD2D5DF);
        _descriptionLabel.font = [UIFont systemFontOfSize:13];
    }
    return _descriptionLabel;
}


- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [UILabel new];
        _dateLabel.textColor = UIColorFromRGB(0xCCD1DB);
        _dateLabel.font = [UIFont systemFontOfSize:13];
    }
    return _dateLabel;
}

- (UIButton *)selectButton
{
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectButton.userInteractionEnabled = NO;
        [_selectButton setImage:[UIImage imageWithName:@"icon_select_unselected"]
                       forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageWithName:@"icon_select_selected"]
                       forState:UIControlStateSelected];
        [_selectButton setImage:[UIImage imageWithName:@"icon_select_selected"]
                       forState:UIControlStateHighlighted];
        
    }
    return _selectButton;
}


#pragma mark - setter
- (void)setCoupon:(PywCoupon *)coupon
{
    _coupon = coupon;
    
    NSInteger amount = [coupon.amount integerValue];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%ld",amount];
    self.dateLabel.text = coupon.coupon_tips;
    _coupon.remainAmount = amount - _coupon.chioceAmount;
    self.descriptionLabel.text = [NSString stringWithFormat:@"使用:￥%ld  剩余:￥%ld",_coupon.chioceAmount,_coupon.remainAmount];
    
    self.moneyLabel.textColor = MoneyColor(coupon.isChoose);
    self.topView.backgroundColor = TopViewColor(coupon.isEnableChoose);
    self.backView.backgroundColor = [UIColor whiteColor];
    
    NSString *description = [NSString stringWithFormat:@"使用:￥%ld  剩余:￥%ld",_coupon.chioceAmount,_coupon.remainAmount];
    NSString *chiceAmount =  [NSString stringWithFormat:@"￥%ld",_coupon.chioceAmount];
    [self updateDescriptionLabelWithText:description
                                 keyText:chiceAmount
                                isChoose:coupon.isChoose];
}

#pragma mark - Action
/**
 *  更新可选不可选的背景状态
 *
 *  @param isEnable <#isEnable description#>
 */
- (void)updateChooseBackgroudColor:(BOOL)isEnable
{
    self.topView.backgroundColor = TopViewColor(isEnable);
}

/**
 *  修改cell选中状态
 *
 *  @param choose <#choose description#>
 */
- (void)updateChooseStatus:(BOOL)choose
{
    if (self.selectPayAmount >= self.orgPayAmount && choose) {
        return;
    }
    self.coupon.isChoose = choose;
    [self calculateAmount:choose];
    self.selectButton.selected = choose;
    self.moneyLabel.textColor = MoneyColor(choose);
//    self.topView.backgroundColor = TopViewColor(choose);
    self.backView.backgroundColor = [UIColor whiteColor];
    NSString *description = [NSString stringWithFormat:@"使用:￥%ld  剩余:￥%ld",self.coupon.chioceAmount,self.coupon.remainAmount];
    NSString *chiceAmount =  [NSString stringWithFormat:@"￥%ld",self.coupon.chioceAmount];
    [self updateDescriptionLabelWithText:description
                                 keyText:chiceAmount
                                isChoose:choose];
    
    
}

/**
 *  计算更改选中状态后的金额
 *
 *  @param choose <#choose description#>
 */
- (void)calculateAmount:(BOOL)choose
{
    NSInteger returnChooseAmount = 0;
    NSInteger couponAmount = [self.coupon.amount integerValue];//代金券金额
    if (self.coupon.isChoose) {
        NSInteger orderRemainAmount = self.orgPayAmount - self.selectPayAmount;//剩余要选择的订单金额
        self.coupon.chioceAmount = couponAmount>orderRemainAmount?orderRemainAmount:couponAmount;//选择金额
        self.coupon.remainAmount = couponAmount - self.coupon.chioceAmount;//代金券剩余金额
        returnChooseAmount = self.coupon.chioceAmount;
    }else {
        returnChooseAmount = - self.coupon.chioceAmount;
        self.coupon.chioceAmount = 0;
        self.coupon.remainAmount = couponAmount - self.coupon.chioceAmount;
    }
    if (self.chooseCoupon) {
        self.chooseCoupon(returnChooseAmount);
    }
    
}

/**
 *  更新使用|剩余金额字体颜色
 *
 *  @param text     <#text description#>
 *  @param keyText  使用金额
 *  @param isChoose 选中
 */
- (void)updateDescriptionLabelWithText:(NSString *)text
                               keyText:(NSString *)keyText
                              isChoose:(BOOL)isChoose
{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
//    [string addAttribute:NSForegroundColorAttributeName
//                   value:UIColorFromRGB(0xD6D6D6).CGColor
//                   range:NSMakeRange(0, text.length)];
//    [string addAttribute:NSFontAttributeName
//                   value:[UIFont systemFontOfSize:13.f]
//                   range:NSMakeRange(0, text.length)];
//    NSRange range = [text rangeOfString:keyText];
//    if (range.length != NSNotFound & isChoose) {
//        [string addAttribute:NSForegroundColorAttributeName
//                       value:UIColorFromRGB(0xF6BA3E).CGColor
//                       range:range];
//    }
    self.descriptionLabel.attributedText = string;
    
}


@end
